a. Get openaAI API Key
b. Call/Check openai APIs using Postman
GET https://api.openai.com/v1/models
Header
Authorization: Bearer API_KEY
POST https://api.openai.com/v1/chat/completions
Header
Authorization: Bearer API_KEY
Body
{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "What is capital of France?"}],
"temperature": 0.7
}
c. Calling APIs using code
Create Virtual Env
// Install openai
(virtual_avatar) c:\go-here\Code\python\virtual_avatar>pip install openai
//Set API Key in User environment variable
OPENAI_API_KEY = value
Call APIs from code
from openai import OpenAI
def get_chatgpt_response():
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content":
"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."},
{"role": "user", "content":
"Compose a poem that explains the concept of recursion in programming."}
]
)
OR
def get_chatgpt_response():
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "What's Capital of Russia?"
}
]
)
print(completion.choices[0].message) //Moscow
get_chatgpt_response()